package com.hntx.joymusic.util;

import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.blankj.utilcode.utils.ToastUtils;
import com.hntx.joymusic.bean.VideoBean;
import com.hntx.joymusic.mediaplayer.IMPlayListener;
import com.hntx.joymusic.mediaplayer.MPlayer;

import java.util.List;

public class PlayUtils {
    private String TAG = "ExitDialog";
    private int startPosition = 0;
    private static PlayUtils instance;
    private String firstUrl = null;

    public static PlayUtils getInstance() {
        synchronized (PlayUtils.class) {
            if (instance == null)
                instance = new PlayUtils();
        }
        return instance;
    }

    private MPlayer player;

    private void play(Context context, final String firstUrl, List<VideoBean> videoBeanList) {
        this.firstUrl = firstUrl;
        player = new MPlayer();
        player.setDisplay(surfaceView);
        playerSetListen(context, videoBeanList);
        if (!TextUtils.isEmpty(firstUrl)) {
            starPlayUrl(firstUrl);
        }
    }

    private void starPlayUrl(String mUrl) {
        if (!TextUtils.isEmpty(mUrl)) {
            if (mUrl.length() > 0) {
                /**2019.9.3 过添加*/
                try {
                    player.setSource(mUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static WindowManager windowAudioManager = null;
    private static WindowManager.LayoutParams audioParams = null;
    private SurfaceView surfaceView;
    private FrameLayout frameLayout;

    public void createAudioLayout(Context context, String firstUrl, List<VideoBean> videoBeanList, int width, int height, int x, int y) {
        if (windowAudioManager == null) {
            windowAudioManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if (audioParams == null) {
            audioParams = new WindowManager.LayoutParams();
            audioParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            audioParams.format = PixelFormat.TRANSLUCENT;
            audioParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        }
        audioParams.width = width;
        audioParams.height = height;
        audioParams.x = x;
        audioParams.y = y;
        audioParams.gravity = Gravity.TOP | Gravity.LEFT;
        if (frameLayout == null) {
            frameLayout = new FrameLayout(context);
            if (surfaceView == null)
                surfaceView = new SurfaceView(context);
            surfaceView.setVisibility(View.VISIBLE);
            frameLayout.addView(surfaceView);
            play(context, firstUrl, videoBeanList);
        }
    }

    public void showAudioLayout(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(context))
                return;
        }
        try {
            if (frameLayout != null) {
//                windowAudioManager.removeView(frameLayout);
                windowAudioManager.removeViewImmediate(frameLayout);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        windowAudioManager.addView(frameLayout, audioParams);
    }

    public void hideAudioLayout() {
        try {
            if (frameLayout != null) {
                Log.d(TAG, "JAVA_JS" + frameLayout);
                player.onDestroy();
//                windowAudioManager.removeView(frameLayout);
                windowAudioManager.removeViewImmediate(frameLayout);
                frameLayout = null;
                surfaceView = null;
            }
        } catch (Exception e) {
            Log.d(TAG, "销毁异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    private void playerSetListen(Context context, List<VideoBean> videoBeanList) {
        player.setPlayListener(new IMPlayListener() {
            @Override
            public void onStart(MPlayer player) {
            }

            @Override
            public void onPause(MPlayer player) {
            }

            @Override
            public void onResume(MPlayer player) {
            }

            @Override
            public void onComplete(MPlayer player) {
                Log.d(TAG, "正onComplete");
                startPosition++;
                if (videoBeanList.size() > startPosition) {
                    Log.d(TAG, "正常onComplete===1");
                    String url = videoBeanList.get(startPosition).getSongRes();
                    starPlayUrl(url);
                } else {
                    startPosition = 0;
                    starPlayUrl(firstUrl);
                }
            }

            @Override
            public void onError(String source, int what, int extra) {
                ToastUtils.showShortToastSafe("视频播放出错，原因：" + what + "=" + source);
            }
        });
    }

    public void audioStop(Context context, boolean isflag) {
        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (isflag) {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
        } else {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_UNMUTE, 0);
        }
    }
}
